﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Siren.MFE.Bll.Core;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using Siren.MFE.Bll.Exception;
using System.Data;
using System.Collections;

namespace Siren.MFE.Bll.SirenField
{
    public class SirenFieldMgr : EntityMgr
    {
        private const string SP_GET_PARENT_SIREN_FIELD_TREE  ="LU_GetSirenFieldTree";
        private const string SP_GET_CHILD_SIREN_FIELDS = "LU_GetSirenFieldChildNodes";

        private const string SP_IN_PARAM_LEVEL = "@Level";
        private const string SP_IN_PARAM_PARENT_ID = "@ParentID";

        private const string COLUMN_ID = "ID";
        private const string COLUMN_PARENTID = "ParentID";
        private const string COLUMN_CAPTION = "Caption";
        private const string COLUMN_QUERYTEXT = "QueryText";
        private const string COLUMN_TOTALNODES = "TotalNodes";

        
        public SirenFieldMgr(String ConnectionString) : base(ConnectionString)
        { 
        
        }

        public List<SirenFieldInfo> GetParentSirenFields(int Level)
        {
            List<SirenFieldInfo> lstSirenFields = new List<SirenFieldInfo>();

            try
            {

                Hashtable htParentNodes = new Hashtable();

                SqlDatabase objDatabase = new SqlDatabase(this.ConnectionString);

                DbCommand dbCmd = objDatabase.GetSqlStringCommand(SP_GET_PARENT_SIREN_FIELD_TREE);

                dbCmd.CommandType = System.Data.CommandType.StoredProcedure;

                objDatabase.AddInParameter(dbCmd, SP_IN_PARAM_LEVEL, DbType.Int32, Level);

                using (IDataReader dataReader = objDatabase.ExecuteReader(dbCmd))
                {
                    while (dataReader.Read())
                    {
                        SirenFieldInfo sirenFieldInfo = new SirenFieldInfo();

                        sirenFieldInfo.ID = Convert.ToString(dataReader[COLUMN_ID]);
                        sirenFieldInfo.ParentID = Convert.ToString(dataReader[COLUMN_PARENTID]);
                        sirenFieldInfo.Caption = Convert.ToString(dataReader[COLUMN_CAPTION]);
                        sirenFieldInfo.QueryText= Convert.ToString(dataReader[COLUMN_QUERYTEXT]);
                        sirenFieldInfo.TotalNodes = Convert.ToInt32(dataReader[COLUMN_TOTALNODES]);

                        htParentNodes[sirenFieldInfo.ID] = sirenFieldInfo;

                        if (htParentNodes.ContainsKey(sirenFieldInfo.ParentID))
                        {
                            SirenFieldInfo sirenFieldParentNode = (SirenFieldInfo)htParentNodes[sirenFieldInfo.ParentID];

                            sirenFieldParentNode.ChildNodes.Add(sirenFieldInfo);

                            htParentNodes[sirenFieldInfo.ParentID] = sirenFieldParentNode;
                        }
                        else
                        {
                            lstSirenFields.Add(sirenFieldInfo);
                        }
                    }
                }

                return lstSirenFields;
            }
            catch (System.Exception exp)
            {
                MFEExceptions mfeExp = new MFEExceptions("", exp);

                throw mfeExp;
            }
        }

        public List<SirenFieldInfo> GetChildSirenFields(String ParentID)
        {
            List<SirenFieldInfo> lstSirenFields = new List<SirenFieldInfo>();

            try
            {
                SqlDatabase objDatabase = new SqlDatabase(this.ConnectionString);

                DbCommand dbCmd = objDatabase.GetSqlStringCommand(SP_GET_CHILD_SIREN_FIELDS);

                dbCmd.CommandType = System.Data.CommandType.StoredProcedure;

                objDatabase.AddInParameter(dbCmd, SP_IN_PARAM_PARENT_ID, DbType.String, ParentID);

                using (IDataReader dataReader = objDatabase.ExecuteReader(dbCmd))
                {
                    while (dataReader.Read())
                    {
                        SirenFieldInfo sirenFieldInfo = new SirenFieldInfo();

                        sirenFieldInfo.ID = Convert.ToString(dataReader[COLUMN_ID]);
                        sirenFieldInfo.ParentID = Convert.ToString(dataReader[COLUMN_PARENTID]);
                        sirenFieldInfo.Caption = Convert.ToString(dataReader[COLUMN_CAPTION]);
                        sirenFieldInfo.QueryText = Convert.ToString(dataReader[COLUMN_QUERYTEXT]);
                        sirenFieldInfo.TotalNodes = Convert.ToInt32(dataReader[COLUMN_TOTALNODES]);

                        lstSirenFields.Add(sirenFieldInfo);

                    }
                }

                return lstSirenFields;
            }
            catch (System.Exception exp)
            {
                MFEExceptions mfeExp = new MFEExceptions("", exp);

                throw mfeExp;
            }
        }
    }

}
